<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/28
 * Time: 下午6:08
 */

namespace app\index\controller;


use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use think\Controller;

class PayBase extends Controller
{
    public function getlptpayinfo($mid){


    }
    public function get_card_userinfo($mid, $openid)
    {
        $Merchant = new Merchant();
        $top_mid = $Merchant->where('id', $mid)->value('top_mid');
        $CardUserGet = new CardUserGet();
        $map = array(
            'top_mid' => $top_mid,
            'openid' => $openid,
            'have' => 1,
            'status' => 1,
            'statu' => 0,
            'card_type' => "MEMBER_CARD",
        );
        $carduserinfo = $CardUserGet->where($map)->find();
        return $carduserinfo;
    }
    public function getdeduzkinfo($arr,$card_zk){
        $arr = json_decode($arr, JSON_UNESCAPED_UNICODE);
        $CardGeneral = new \app\common\model\CardGeneral();
        if (is_array($arr)) {
            foreach ($arr as $k => $v) {
                if (isset($v['card_id'])) {
                    $datas[$k] = $CardGeneral->where(['card_id' => $v['card_id']])->value('title');
                } else {
                    $datas = [];
                }
            }
        }else{
            $datas = [];
        }

        if ($datas) {
            $data = implode(',', $datas).'('.$card_zk.'元)';
        } else {
            $data = NULL;
        }
        return $data;
    }
    public function get_api_data($param)
    {
        unset($param['type']);

        $Agent = new Agent();
        $Mechant = new Merchant();
        $agent_id = $Mechant->where(array('id' => $param['merchantid']))->value('agent');
        $appid = $Agent->where(array('id' => $agent_id))->value('sign_appid');
        $data = $param;

        $data['appid'] = $appid;
        $data['version'] = "2.0";
        $data['timestamp'] = time();
        $data['signaturenonce'] = MD5(rand(1000000, 9999999));
        $data['sign'] = getsign(uni_array($data), "MD5", $data['appid']);

        return json_encode($data);

    }
    public function getcarduserinfo($params)
    {

        $Merchant = new Merchant();
        $top_mid = $Merchant->where('id', $params['merchantid'])->value('top_mid');
        $midlist = $Merchant->where('top_mid', $top_mid)->field('id')->select();
        foreach ($midlist as $k => $v) {
            $midarr[] = $v['id'];
        }
        $mid = implode(',', $midarr);
        $map['mid'] = ['in', $mid];
        $map['top_mid'] = $top_mid;
        $map['openid'] = $params['openid'];
        $map['have'] = 1;
        $map['fire'] = 1;
        $map['status'] = 1;
        $map['card_type'] = 'MEMBER_CARD';
        $Wxusergetcard = new CardUserGet();
        $userinfo = $Wxusergetcard->where($map)->find();
        if ($userinfo) {
            $re_param = array('result_code' => "SUCCESS", 'code' => $userinfo['code'],'card_id' =>$userinfo['card_id'], "balance" => $userinfo['balance']);
        } else {
            $CardMember = new CardMember();
            $card_id = $CardMember->where(array('top_mid' => $top_mid))->value('card_id');

            $re_param = array('result_code' => "FAIL", 'card_id' => $card_id);

        }
        return $re_param;


    }

    public function getcardinfo($params)
    {

        $Merchant = new Merchant();
        $top_mid = $Merchant->where('id', $params['merchantid'])->value('top_mid');
        $CardMember = new CardMember();
        $cardinfo = $CardMember->where(array('top_mid' => $top_mid))->field('card_id,is_pay_putcard,id')->find();
        if ($cardinfo){
            $re_param['result_code'] = 'SUCCESS';
            $re_param['data'] = $cardinfo['is_pay_putcard'];
        }else{
            $re_param['result_code'] = 'ERROR';
            $re_param['data'] = '';
        }
        return $re_param;
    }

    public function http_post($param, $url = 'http://api.wunengkeji.com/api.php', $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {

            if ($is_curlFile) {
                foreach ($param as $key => $val) {

                    if (substr($val, 0, 1) == '@') {

                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));

                    }
                }

            }
            $strPOST = $param;

        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);

        }
//        return $url;
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
            'header' => 'Content-Type:application/json; charset=utf-8',
            'Content-Length: ' . strlen($param)));
        $sContent = curl_exec($oCurl);
//        file_put_contents('/data2/html/payos/post.txt', $sContent . PHP_EOL, FILE_APPEND);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
//        return $aStatus;
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}